package gateway.behaviours;

import gateway.GatewayAgent;
import gateway.ontology.classes.basic_translation.ParamNotFoundException;

import java.util.ArrayList;
import java.util.LinkedList;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;

import com.Constants;
import com.Event;
import com.ParamsLabel;
import com.sun.spot.io.j2me.radiogram.RadiogramConnection;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;

public class CommunicationChannelSenderBehaviour extends CyclicBehaviour {

	private LinkedList<Event> events = new LinkedList<Event>();
	private String communicationPort;

	public CommunicationChannelSenderBehaviour(String communicationPort,
			Agent agent) {
		super(agent);
		this.communicationPort = communicationPort;
	}

	@Override
	public void action() {
		Event event = null;

		synchronized (events) {
			while ((event = events.poll()) == null) {
				try {
					events.wait();
				} catch (InterruptedException e) {
					// do nothing
				}
			}
		}

		byte evtName = event.getName();
		RadiogramConnection conn = null;
		Datagram dg = null;
		try {
			switch (evtName) {
			case Event.MGR_REQUEST:
			case Event.MGR_ACK:
			case Event.MGR_EXECUTED:
				// not needed
				break;
			case Event.DSC_PUBLISH:
				conn = (RadiogramConnection) Connector
						.open("radiogram://broadcast:" + this.communicationPort);
				dg = conn.newDatagram(conn.getMaximumLength());
				dg.writeUTF(evtName + Constants.COMMUNICATION_SEPARATOR);
				break;
			case Event.DSC_ANSWER:
				conn = (RadiogramConnection) Connector.open("radiogram://"
						+ event.getTargetID() + ":" + this.communicationPort);
				dg = conn.newDatagram(conn.getMaximumLength());
				dg.writeUTF(evtName + this.agentsToString());
				break;
			case Event.DSC_REFRESH:
				conn = (RadiogramConnection) Connector
						.open("radiogram://broadcast:" + this.communicationPort);
				dg = conn.newDatagram(conn.getMaximumLength());
				dg.writeUTF(evtName + Constants.COMMUNICATION_SEPARATOR
						+ event.getParam(ParamsLabel.AGT_NAME));
				break;
			case Event.MSG_TO_BASESTATION:
				// not needed
				break;
			case Event.AGN_CREATION:
			case Event.MSG:
			default:
				conn = (RadiogramConnection) Connector.open("radiogram://"
						+ event.getParam(ParamsLabel.AGT_ADDRESS) + ":"
						+ this.communicationPort);
				dg = conn.newDatagram(conn.getMaximumLength());
				dg.writeUTF(evtName + Constants.COMMUNICATION_SEPARATOR
						+ event.toString());
				break;
			}
			conn.send(dg);
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void put(Event event) {
		synchronized (events) {
			events.add(event);
			events.notifyAll();
		}
	}

	private String agentsToString() {
		GatewayAgent gateway = (GatewayAgent) this.myAgent;
		ArrayList<String> agents = gateway.getLocalMAPSagents();
		String stringAgents = "";
		if (agents.size() > 0) {
			stringAgents = Constants.COMMUNICATION_SEPARATOR;
			for (String aid : agents) {
				stringAgents += aid + Constants.COMMUNICATION_SEPARATOR;
			}
		}
		return stringAgents;
	}
}
